﻿using BLL;
using Model;
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
//using System.Linq
//using System.Collections.Generic
//using System.ComponentModel
//using System.Drawing
//using System.Text
//using System.Data.SqlClient
//using System.Data.OleDb
//using System.Configuration
//using NCRE学生考试端V1._0
//using NCRE学生考试端V1._0.教师端操作类

namespace NCRE学生考试端V1._0.教师端操作类
{
    /// <summary>
    /// 将显示成绩窗体修改到此界面--杨光彩-2018-2-22 20:08:38
    /// </summary>
    public partial class frmDisplayScore : Form
    {
        public frmDisplayScore()
        {
            InitializeComponent();
        }

        #region 窗体单一显示-李金颖-2018-12-8 12:05:22
        private static frmDisplayScore FrmDisplayScore = null;
        public static frmDisplayScore GetInstance()
        {
            if (FrmDisplayScore==null|| FrmDisplayScore.IsDisposed)
            {
                FrmDisplayScore = new frmDisplayScore();
            }
            return FrmDisplayScore;
        }
        #endregion

        //定义一个学生分数的逻辑类
        StudentScoreBLL studentscorebll = new StudentScoreBLL();

        //定义一个学生的实体用来传递学生信息
        StudentInfoEntity studentinfo = new StudentInfoEntity();
        #region 注释代码
        //定义一个ExamConfigBLL类
        //ExamConfigBll examconfigbll = new ExamConfigBll()

        //定义一个WordQuestionEntityBLL类
        //WordQuestionEntityBll wordquestionbll = new WordQuestionEntityBll()

        //定义一个WinQuestionEntityBll类
        //WinQuestionEntityBll winquestionbll = new WinQuestionEntityBll()

        //定义一个ExcelQuestionEntityBLL类
        //ExcelEntityBll excelQuestionBll = new ExcelEntityBll()

        //定义一个IEBLL类
        //IeQuestionEntityBll iequestionbll = new IeQuestionEntityBll()

        //定义一个StudentBindPaperTypeBLL类
        //StudentBindPaperTypeBLL studentBindPaperType = new StudentBindPaperTypeBLL()
        //定义一个StudentBindPaperTypeEntity类
        //StudentBindPaperTypeEntity studentBindPaperTypeEntity = new StudentBindPaperTypeEntity()

        //定义一个PPTBLL类
        //PptQuestionEntityBLL pptQuestionBll = new PptQuestionEntityBLL()

        //定义一个学生类
        //StudentInfoBll studentinfoBll = new StudentInfoBll()
        #endregion

        #region 绑定窗体加载时需要的数据--周洲--2015年11月16日   
        /// <summary>
        /// 绑定窗体加载时需要的数据--周洲--2015年11月16日
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        DataTable dtcollege = null;
        private void frmDisplayScore_Load(object sender, EventArgs e)
        {
            #region 设置开始时间提前两小时，截至时间晚两小时
            dateTimePicker1.Value = DateTime.Now.AddHours(-2);
            dateTimePicker2.Value = DateTime.Now.AddHours(2);
            #endregion
            //调用方法选择所有的专业
            dtcollege = studentscorebll.SelectAllCollege();
            //为专业的combobox控件赋值
            cboCollege.DataSource = dtcollege;
            cboCollege.DisplayMember = "学院";
            cboCollege.ValueMember = "collegeName";

            //将下拉框中的值传进studentinfo中
            studentinfo.major = "汉语言文学";
        }
        #endregion

        #region 在选择学院的时候选择该专业下的所有学生  
        /// <summary>
        /// 在选择学院的时候选择该专业下的所有学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboCollege_SelectedIndexChanged_1(object sender, EventArgs e)
        {
             switch (cboCollege.Text)
            {
                case "文学院":
                    studentinfo.CollegeID = "01";
                    break;
                case "社会发展学院":
                    studentinfo.CollegeID = "02";
                    break;
                case "外国语学院":
                    studentinfo.CollegeID = "03";
                    break;
                case "管理学院":
                    studentinfo.CollegeID = "04";
                    break;
                case "美术学院":
                    studentinfo.CollegeID = "05";
                    break;
                case "音乐学院":
                    studentinfo.CollegeID = "06";
                    break;
                case "教育学院":
                    studentinfo.CollegeID = "07";
                    break;
                case "电子信息工程学院":
                    studentinfo.CollegeID = "08";
                    break;
                case "理学院":
                    studentinfo.CollegeID = "09";
                    break;
                case "化学与材料科学学院":
                    studentinfo.CollegeID = "10";
                    break;
                case "生命科学学院":
                    studentinfo.CollegeID = "11";
                    break;
                case "经济学院":
                    studentinfo.CollegeID = "12";
                    break;
                case "建筑工程学院":
                    studentinfo.CollegeID = "13";
                    break;
                case "体育学院":
                    studentinfo.CollegeID = "14";
                    break;
                case "传媒系":
                    studentinfo.CollegeID = "15";
                    break;
                case "其他":
                    studentinfo.CollegeID = "16";
                    break;
                default:
                    studentinfo.CollegeID = "01";
                    break;
            }

            //调用方法选择所有的专业
            DataTable dtmajor = studentscorebll.SelectMajorByCollegeID(studentinfo);
            //为专业的combobox控件赋值
            cboMajor.DataSource = dtmajor;
            cboMajor.DisplayMember = "专业";
            cboMajor.ValueMember = "major";

            if (dtmajor.Rows.Count == 0)
            {
                MessageBox.Show("该学院无数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                while (this.dgvScore.Rows.Count != 0)
                {
                    this.dgvScore.Rows.RemoveAt(0);
                }
            }
        }
        #endregion

        #region 导出成绩Excel表
        private void btnExcel_Click(object sender, EventArgs e)
        {
            
            if (dgvScore.RowCount==0)
            {
                MessageBox.Show("成绩单为空", "提示", MessageBoxButtons.OK,
                MessageBoxIcon.Warning);
            }
            else
            {                               
                dgvScore.DataSource = datasource2;
                ExcelExport excelexcport = new ExcelExport();
                excelexcport.setExcel(dgvScore, cboMajor.Text.ToString());
                //导出成功后进行分页
                dgvScore.AllowUserToAddRows = false;
                cRows = datasource2.Rows.Count;
                cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                PageCurrent = 1;
                record = cRows;
                LoadData();
            }
        }
        #endregion


        #region 在选择专业的时候选择该专业下的所有学生       
        /// <summary>
        /// 在选择专业的时候选择该专业下的所有学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> 
        DataTable datasource2 = new DataTable();
        private void btnInqueryScore_Click(object sender, EventArgs e)
        {
            //将下拉框中的值传进studentinfo中
            studentinfo.major = cboMajor.Text.Trim();
            switch (cboCollege.Text)
            {
                case "文学院":
                    studentinfo.CollegeID = "01";
                    break;
                case "社会发展学院":
                    studentinfo.CollegeID = "02";
                    break;
                case "外国语学院":
                    studentinfo.CollegeID = "03";
                    break;
                case "管理学院":
                    studentinfo.CollegeID = "04";
                    break;
                case "美术学院":
                    studentinfo.CollegeID = "05";
                    break;
                case "音乐学院":
                    studentinfo.CollegeID = "06";
                    break;
                case "教育学院":
                    studentinfo.CollegeID = "07";
                    break;
                case "电子信息工程学院":
                    studentinfo.CollegeID = "08";
                    break;
                case "理学院":
                    studentinfo.CollegeID = "09";
                    break;
                case "化学与材料科学学院":
                    studentinfo.CollegeID = "10";
                    break;
                case "生命科学学院":
                    studentinfo.CollegeID = "11";
                    break;
                case "经济学院":
                    studentinfo.CollegeID = "12";
                    break;
                case "建筑工程学院":
                    studentinfo.CollegeID = "13";
                    break;
                case "体育学院":
                    studentinfo.CollegeID = "14";
                    break;
                case "传媒系":
                    studentinfo.CollegeID = "15";
                    break;
                case "其他":
                    studentinfo.CollegeID = "16";
                    break;
                default:
                    studentinfo.CollegeID = "01";
                    break;
            }
            
            string starttime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            string endtime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
            
            //清空datatable表
            datasource2.Clear();
            datasource2.Columns.Clear();
            datasource2.Columns.Add("姓名", typeof(string));
            datasource2.Columns.Add("学号", typeof(string));
            datasource2.Columns.Add("总分", typeof(string));
            datasource2.Columns.Add("选择题", typeof(string));
            datasource2.Columns.Add("Word", typeof(string));
            datasource2.Columns.Add("Excel", typeof(string));
            datasource2.Columns.Add("IE", typeof(string));
            datasource2.Columns.Add("Windows", typeof(string));
            datasource2.Columns.Add("PPT", typeof(string));
            datasource2.Columns.Add("日期", typeof(string));
            datasource2.Columns.Add("模式", typeof(string));

            //选择学生的操作模式——陈新月——2018-03-26 -修改人：谷海涛-2018年12月28日19:01:44
            DataTable dataIsexam = studentscorebll.SelectIsexam();

            ////查询一段时间内的考生各科成绩及总分 修改人：李金颖-更改方法说明-2018-12-5 10:11:40
            //选择学生的最高分
            DataTable datasource = studentscorebll.SelectTopScoreByMajor(studentinfo, starttime, endtime);

            datasource.Columns.Add("模式", typeof(string));

            foreach (DataRow dr in datasource.Rows)
            {
                //if (dataIsexam.Rows[0]["模式"].ToString() == "0")

                //修改查询条件为is_upload（上传）这个字段-谷海涛-2018年12月28日19:01:01
                if (dataIsexam.Rows[0]["上传"].ToString() == "0")
                {
                    dr["模式"] = "考试模式";
                }
                else
                {
                    dr["模式"] = "练习模式";
                }

            }
            foreach (DataRow dr in datasource.Rows)
            {
                datasource2.ImportRow(dr);

            }

            if (datasource2.Rows.Count > 0)
            {
                //分页
                dgvScore.AllowUserToAddRows = false;
                cRows = datasource2.Rows.Count;
                cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                PageCurrent = 1;
                record = cRows;
                LoadData();
            }
            else
            {
                dgvScore.DataSource = "";
                MessageBox.Show("没有成绩记录！");
            }

        }
        #endregion

        #region 分页功能-吕德彪-2018
        DataTable table1 = new DataTable();
        //定义分页需要的变量
        int cRows = 0;
        int RowCurrent = 0;
        int cPages = 0;
        int PageCurrent = 0;
        const int PageSize = 10;
        int record = 0;


        private void LoadData()
        {
            if (PageCurrent == 1)
            {
                ts_PagePrevious.Enabled = false;
            }
            else
            {
                ts_PagePrevious.Enabled = true;
            }
            if (cPages == PageCurrent)
            {
                ts_PageNext.Enabled = false;
            }
            else
            {
                ts_PageNext.Enabled = true;
            }
            toolStripLabel3.Text = record.ToString();
            ts_PagePostion.Text = PageCurrent.ToString();
            ts_Pages.Text = cPages.ToString();
            int startRow = (PageCurrent - 1) * PageSize;
            int endRow = PageCurrent == cPages ? cRows : PageCurrent * PageSize;
            DataTable dtTemp = datasource2.Clone();
            if (datasource2.Rows.Count != 0)
            {
                for (int i = startRow; i < endRow; i++)
                {
                    dtTemp.ImportRow(datasource2.Rows[i]);
                }
                bindingSource1.DataSource = null;
                bindingSource1.DataSource = dtTemp;
                dgvScore.AllowUserToAddRows = false;
                dgvScore.DataSource = bindingSource1;

            }

        }

        private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "上一页")
            {
                ts_PageNext.Enabled = true;
                if (PageCurrent == 1)
                {
                    ts_PagePrevious.Enabled = false;
                    MessageBox.Show("已经是第一页了");
                    return;
                }
                PageCurrent--;
                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                ts_PagePrevious.Enabled = true;
                if (PageCurrent == PageSize)
                {
                    ts_PageNext.Enabled = false;
                    MessageBox.Show("已经是最后一页了");
                    return;
                }
                PageCurrent++;
                LoadData();
            }
        }

        private void bindingNavigatorMoveFirstPage_Click(object sender, EventArgs e)
        {
            PageCurrent = 1;
            bindingNavigatorMoveFirstPage.Enabled = false;
            bindingNavigatorMoveLastPage.Enabled = true;
            ts_PagePrevious.Enabled = false;
            ts_PageNext.Enabled = true;
            LoadData();
        }

        private void bindingNavigatorMoveLastPage_Click(object sender, EventArgs e)
        {
            PageCurrent = cPages;
            bindingNavigatorMoveFirstPage.Enabled = true;
            bindingNavigatorMoveLastPage.Enabled = false;
            ts_PagePrevious.Enabled = true;
            ts_PageNext.Enabled = false;
            LoadData();
        }

        private void ts_PagePostion_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(ts_PagePostion.Text) > 0 && Convert.ToInt32(ts_PagePostion.Text) <= cPages)
                {
                    PageCurrent = Convert.ToInt32(ts_PagePostion.Text);
                    LoadData();
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (System.Exception ex)
            {
                PageCurrent = 1;
                LoadData();
            }
        }
        #endregion

    }
}


